package com.recipefinder.service;

import java.util.List;

import com.recipefinder.bean.Ingredient;
import com.recipefinder.bean.Recipe;

public class IngredientCheckService {
	public boolean checkIfRecipeSatisfyWithIngredients(Recipe recipe, 
			List<Ingredient> ingredientList){
		
		List<Ingredient> recipeRequiredIngredients = recipe.getIngredients();
		for(Ingredient requiredIngredient : recipeRequiredIngredients){
			Ingredient matchedIngredient = checkIfIngredientSatisfied(requiredIngredient, ingredientList);
			if(matchedIngredient == null)
				return false;
			else
				recipe.getSatisfiedIngredients().add(matchedIngredient);
		}
		return true;
	}
	
	public Ingredient checkIfIngredientSatisfied(Ingredient requiredIngredient, List<Ingredient> ingredientList){
		boolean hasMeet = false;
		for(Ingredient provideIngredient : ingredientList){
			boolean itemHasMeet = requiredIngredient.getItem().equals(provideIngredient.getItem());
			boolean amountHasMeet = requiredIngredient.getAmount() <= provideIngredient.getAmount();
			
			hasMeet = amountHasMeet && itemHasMeet;
			
			if(!hasMeet)
				continue;
			else{
				return provideIngredient;
			}
		}
		return null;
	}
}
